<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/4/12 0012
 * Time: 下午 11:45
 */

namespace Admin\Logic;

class DbmysqlLogic implements DbMysqlinterface {

    /**
     * DB connect
     *
     * @access public
     *
     * @return resource connection link
     */
    public function connect() {
        echo __METHOD__ . '<br/>';
    }

    /**
     * Disconnect from DB
     *
     * @access public
     *
     * @return viod
     */
    public function disconnect() {
        echo __METHOD__ . '<br/>';
    }

    /**
     * Free result
     *
     * @access public
     * @param resource $result query resourse
     *
     * @return viod
     */
    public function free($result) {
        echo __METHOD__ . '<br/>';
    }

    /**
     * Execute simple query
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return resource|bool query result
     */
    public function query($sql, array $args = array()) {
        //获取数据
        $arrs = func_get_args();
        //将数组第一个元素弹出
        $sql = array_shift($arrs);
        //利用正则替换
        $sqlarr = preg_split('/\?[NFT]/', $sql);
        //移出最后一个元素
        array_pop($sqlarr);
        //循环拼接sql语句
        $sql = '';
        foreach ($sqlarr as $key => $value) {
            $sql .= $value . $arrs[$key];
        }
        M()->execute($sql);
    }

    /**
     * Insert query method
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return int|false last insert id
     */
    public function insert($sql, array $args = array()) {
        //获取到数组结构
        $arrs = func_get_args();
        //取出sql语句
        $sql = $arrs[0];
        //表名
        $tablename = $arrs[1];
        //内容
        $arr = $arrs[2];
        //删除主键id
         unset($arr['id']);
        //利用字符串替换
        $sql = str_replace('?T', $tablename, $sql);
        $find = array();
        foreach ($arr as $key => $value) {
            $find[] = '`' . $key . '`="' . $value . '"';
        }
        $field_str = implode(',', $find);
        $sql = str_replace('?%', $field_str, $sql);
        if (M()->execute($sql) !== false) {
            return M()->getLastInsID();
        } else {
            return false;
        }
    }

    /**
     * Update query method
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return int|false affected rows
     */
    public function update($sql, array $args = array()) {
        echo __METHOD__ . '<br/>';
    }

    /**
     * Get all query result rows as associated array
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return array associated data array (two level array)
     */
    public function getAll($sql, array $args = array()) {
        echo __METHOD__ . '<br/>';
    }

    /**
     * Get all query result rows as associated array with first field as row key
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return array associated data array (two level array)
     */
    public function getAssoc($sql, array $args = array()) {
        echo __METHOD__ . '<br/>';
    }

    /**
     * Get only first row from query
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return array associated data array
     */
    public function getRow($sql, array $args = array()) {
        //获取数据
        $arrs = func_get_args();
        //将数组的第一个元素弹出
        $sql = array_shift($arrs);
        //拆分sql语句成数组
        $sqlarr = preg_split('/\?[NTF]/', $sql);
        //移出最后一个元素
        array_pop($sqlarr);
        //循环拼接sql语句
        $sql = '';
        foreach ($sqlarr as $key => $value) {
            $sql .= $value . $arrs[$key];
        }
        //执行sql  query获取返回内容 execute获取影响行数
        $rows = M()->query($sql);
        if ($rows) {
            //获取一行数据
            return array_shift($rows);
        }
    }

    /**
     * Get first column of query result
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return array one level data array
     */
    public function getCol($sql, array $args = array()) {
        echo __METHOD__ . '<br/>';
    }

    /**
     * Get one first field value from query result
     *
     * @access public
     * @param string $sql SQL query
     * @param array $args query arguments
     *
     * @return string field value
     */
    public function getOne($sql, array $args = array()) {
    }

}
